<?php

    class Artikel extends MY_Controller {
        
        function artikel(){ //constructor
            
            parent::__construct();    
            
            //load helpers
            $this->load->helper('url');
            $this->load->helper('form');
            $this->load->library('form_validation');
        }
        
        
        //display all articles
       function index(){
           $this->data['title'] = 'Talisa Blog';
           $this->data['menu'] = 'article';
           $this->data['page'] = 'artikel_view';
           $this->data['icon'] = array('file'=>'article.png','name'=>'artikel');
           $this->data['css'] = array('artikel.css'=>'');
        
           //load pagination library 
            $this->load->library('pagination');
            
            //set pagination config
            $config['base_url'] = base_url() . 'index.php/artikel/index/';
            $config['total_rows'] = $this->db->count_all_results('artikel');
            $config['per_page'] = 3;
            $config['num_links'] = 2;
         //   $config['first_link'] = 'First';
          //  $config['last_link'] = 'Last';
            //$config['full_tag_open'] = '<div>';
            //$config['full_tag_close'] = '</div>';
            
            //initializa pagination
            $this->pagination->initialize($config);
           
           
            //load model
            $this->load->model('artikel_model');
            $this->data['kumpulan'] = $this->artikel_model->getAllArtikel(3,$this->uri->segment(3));
            $this->data['pagination_links'] = $this->pagination->create_links();
       
            //load View
            $this->load->view('admin_view', $this->data);
           
       }
       
       function artikel_detail(){
           
           //load model
           $this->load->model('artikel_model');
           
           //call the function to load article from database
           $data['artikel_details'] = $this->artikel_model->getDetailArtikel($this->uri->segment(3));
           
           
           //load artikel/blog details
           $this->load->view('artikel_detail_view',$data);
           
           
       }
      
       
   function insertNewArtikel(){
           
           
           $this->load->model('artikel_model');
           $post = $this->input->post();
           
           
           if($post){
                // Validation rules. Format: set_rules(input_name, label_name, rules)
            $this->form_validation->set_rules('title', 'Judul', 'required');
            $this->form_validation->set_rules('content', 'Isi', 'required');

            // Set error container
            $this->form_validation->set_error_delimiters('<span class="error">', '</span><br/>');

            // When valid
            if ($this->form_validation->run()) {
                // Set insert data here
                $data = array(
                    'artikel_name' => $post['title'],
                    'artikel_content' => $post['content'],
                    //'type' => $post['type'],
                );

                $success = $this->artikel_model->insertArtikel($data);

                if($success) redirect('artikel/success');
               
               
            }
           
             
        }
        
           // Set form data here
            $form_data = array(
                'title' => set_value('title'),
                'content' => set_value('content')
            );
            $this->data['form_data'] = $form_data;

            $this->load->view('insert_artikel', $this->data);
           
    }
    
    
    function success(){
        echo "Data berhasil dimasukkan";
    }
       
      
        
   }
?>
